﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using OF.Common;
using OF.Cache;
namespace OF.BLL.Addr
{
    public class AreaCache
    {
        ICache cache = CacheFactory.GetCache();
        #region 根据区域ID获取名称
        /// <summary>
        /// 根据key获取参数数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetAreaNameById(int id)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_AREA);
            if (obj != null)
            {
                List<OF.Model.Area> list = ((List<OF.Model.Area>)obj).Where(area => area.id == id).ToList();
                if (list != null && list.Count > 0)
                    return list[0].area;
            }
            return null;
        }

        #endregion

        #region 根据上级获取参数数据
        /// <summary>
        /// 根据key获取参数数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.Area> GetAreaByPid(int pid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_AREA);
            if (obj != null)
            {
                List<OF.Model.Area> list = ((List<OF.Model.Area>)obj).Where(area => area.parentid == pid).ToList();
                if (list != null && list.Count > 0)
                    return list;
                else
                    return null;
            }
            return null;
        }

        #endregion

        #region 根据名称获取学校
        /// <summary>
        /// 根据名称获取学校
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.School> GetSchoolsByName(string name)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_SCHOOL);
            if (obj != null)
            {
                List<OF.Model.School> list = null;
                if (string.IsNullOrWhiteSpace(name))
                {
                    list = ((List<OF.Model.School>)obj).ToList();
                }
                else
                {
                    list = ((List<OF.Model.School>)obj).Where(school => school.shcoolname.Contains(name)).ToList();
                }

                if (list != null && list.Count > 0)
                    return list;
                else
                    return null;
            }
            return null;
        }

        #endregion

        #region 根据id获取学校
        /// <summary>
        /// 根据id获取学校
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public OF.Model.School GetSchoolById(int id)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_SCHOOL);
            if (obj != null)
            {
                List<OF.Model.School> list = null;
                list = ((List<OF.Model.School>)obj).Where(school => school.id == id).ToList();
                if (list != null && list.Count > 0)
                    return list[0];
                else
                    return null;
            }
            return null;
        }

        #endregion

        #region 根据学校获取商家
        /// <summary>
        /// 根据学校获取商家
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.Seller> GetSellersBySchoolId(int schoolid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_SELLERBELONG);
            if (obj != null)
            {
                //根据学校id获取附近商家id
                var temp = ((List<OF.Model.SellerBelong>)obj).Where(sb => sb.schoolid == schoolid).Select(sb => sb.sellerid).Distinct().ToList();
                obj = GetCache(Enmus.CACHE_TYPE.C_SELLER);
                if (obj != null)
                {
                    //查询商家信息
                    List<OF.Model.Seller> list = ((List<OF.Model.Seller>)obj).Where(seller => temp.Contains(seller.id)).ToList();
                    if (list != null && list.Count > 0)
                        return list;
                }
                return null;
            }
            return null;
        }

        #endregion

        #region 根据商家名称获取商家
        /// <summary>
        /// 根据商家名称获取商家
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.Seller> GetSellersByName(string name)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_SELLER);
            if (obj != null)
            {
                //查询商家信息
                List<OF.Model.Seller> list = ((List<OF.Model.Seller>)obj).Where(seller => seller.shopname.Contains(name)).ToList();
                if (list != null && list.Count > 0)
                    return list;
            }

            return null;
        }

        #endregion

        #region 根据区域获取学校
        /// <summary>
        /// 根据区域获取学校
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.School> GetSchoolsByAreaId(int areaid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_SCHOOL);
            if (obj != null)
            {
                List<OF.Model.School> list = ((List<OF.Model.School>)obj).Where(sc => sc.areaid == areaid).ToList();
                if (list != null && list.Count > 0)
                    return list;
            }
            return null;
        }

        #endregion

        #region 根据商家获取菜品
        /// <summary>
        /// 根据商家获取菜品
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<OF.Model.Dish> GetDishsBySellerId(int sellerid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_DISH);
            if (obj != null)
            {
                List<OF.Model.Dish> list = ((List<OF.Model.Dish>)obj).Where(sc => sc.sellerid == sellerid).ToList();
                if (list != null && list.Count > 0)
                    return list;
            }
            return null;
        }

        #endregion

        #region 根据菜品ID获取菜品详情
        /// <summary>
        /// 根据菜品ID获取菜品详情
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public OF.Model.Dish GetDishInfoById(int dishid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_DISH);
            if (obj != null)
            {
                List<OF.Model.Dish> list = ((List<OF.Model.Dish>)obj).Where(sc => sc.id == dishid).ToList();
                if (list != null && list.Count > 0)
                    return list[0];
            }
            return null;
        }

        #endregion

        #region 根据类别ID获取菜品类别
        /// <summary>
        /// 根据类别ID获取菜品类别
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public OF.Model.DishCarts GetCartInfoById(int cartid)
        {
            object obj = GetCache(Enmus.CACHE_TYPE.C_DISHCAT);
            if (obj != null)
            {
                List<OF.Model.DishCarts> list = ((List<OF.Model.DishCarts>)obj).Where(sc => sc.id == cartid).ToList();
                if (list != null && list.Count > 0)
                    return list[0];
            }
            return null;
        }

        #endregion

        #region 获取信息

        /// <summary>
        /// 获取站点信息
        /// </summary>
        /// <returns>返回值是object类型,需要现式转换成OF.Model.Area类型,返还值为null则表示无站点缓存</returns>
        public object GetCache(Enmus.CACHE_TYPE type)
        {
            object res = null;

            if (IsCacheExist(type))
            {
                res = cache.Get(type.ToString());
            }
            return res;
        }
        #endregion

        #region 缓存

        /// <summary>
        /// 判断
        /// </summary>
        /// <returns></returns>
        public bool IsCacheExist(Enmus.CACHE_TYPE type)
        {
            if (cache.Get(type.ToString()) != null)
            {
                return true;
            }
            else
            {
                //区域
                if (type == Enmus.CACHE_TYPE.C_AREA)
                {
                    DAL.Area dal = new DAL.Area();
                    List<OF.Model.Area> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }
                //学校
                else if (type == Enmus.CACHE_TYPE.C_SCHOOL)
                {
                    DAL.School dal = new DAL.School();
                    List<OF.Model.School> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }
                //商家附近学校
                else if (type == Enmus.CACHE_TYPE.C_SELLERBELONG)
                {
                    DAL.SellerBelong dal = new DAL.SellerBelong();
                    List<OF.Model.SellerBelong> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }
                //商家
                else if (type == Enmus.CACHE_TYPE.C_SELLER)
                {
                    DAL.Seller dal = new DAL.Seller();
                    List<OF.Model.Seller> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }
                //商家菜品
                else if (type == Enmus.CACHE_TYPE.C_DISH)
                {
                    DAL.Dish dal = new DAL.Dish();
                    List<OF.Model.Dish> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }   //菜品类别
                else if (type == Enmus.CACHE_TYPE.C_DISHCAT)
                {
                    DAL.DishCarts dal = new DAL.DishCarts();
                    List<OF.Model.DishCarts> list = dal.GetModelList("");
                    if (list != null && list.Count > 0)
                    {
                        AddCache(type, list);
                        return true;
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// 将站点信息加入缓存
        /// </summary>
        /// <param name="site"></param>
        public void AddCache(Enmus.CACHE_TYPE type, object obj)
        {
            cache.Set(type.ToString(), obj, DateTime.Now.AddMinutes(60 * 2));
        }

        /// <summary>
        /// 删除站点缓存
        /// </summary>
        public void RemoveCache(Enmus.CACHE_TYPE type)
        {
            cache.Delete(type.ToString());
        }

        /// <summary>
        /// 更新站点缓存
        /// </summary>
        public void UpdateCache(Enmus.CACHE_TYPE type)
        {
            RemoveCache(type);
            IsCacheExist(type);
        }
        #endregion

    }
}
